package com.dense.kuiniu.shelfcard.business;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.DeviceUtils;
import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.base.Url;
import com.dense.kuiniu.shelfcard.card.idcard.RetryWithDelay;
import com.dense.kuiniu.shelfcard.utils.timer.CountDownView;
import com.dense.kuiniu.shelfcard.utils.timer.CounterController;
import com.rxjava.rxlife.RxLife;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/9/8
 */
public class PayDelegate extends BaseFragment {
    public static PayDelegate getInstance() {
        return new PayDelegate();
    }

    @BindView(R.id.count_down_view)
    CountDownView mCountDownView;
    @BindView(R.id.iv_code)
    ImageView mIvCode;
    @BindView(R.id.tv_pay_msg)
    TextView mTvPayMsg;
    @BindView(R.id.tv_pay_amount)
    TextView mTvPayAmount;
    private Disposable mDisposable;

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_code_pay;
    }

    @OnClick({R.id.tv_exit})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    public void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_exit:
                exit();
                break;
        }
    }

    private void exit() {

        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        pop();
    }

    @Override
    protected void init() {
        mCountDownView.start(new CounterController.OnCompleteListener() {
            @Override
            public void onComplete() {
                exit();
            }
        });
        RxHttp.postForm(Url.GetAlipayCode)
                .add("deviceId", DeviceUtils.getAndroidID())
                .add("level", "1")
                .asString()
                .observeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(String s) {
                        if (getActivity() != null) {
                            Resources resources = getActivity().getResources();
                            Bitmap logo = BitmapFactory.decodeResource(resources, R.mipmap.ic_pay_alipay);
                            Bitmap code = QRCodeEncoder.syncEncodeQRCode(s, 200, Color.BLACK);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mTvPayMsg.setText("请用支付宝扫描下方二维码");
                                    String str = "请支付" + "<font color= \"#FFA61A\"><big>" + " ￥10 " + "</big></font>" + "元";
                                    mTvPayAmount.setText(Html.fromHtml(str));
                                    mIvCode.setImageBitmap(code);
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        //请求失败
                    }

                    @Override
                    public void onComplete() {
                        //请求完成，仅在成功时才会回调
                    }
                });

    }

    private void queryResult() {
        RxHttp.get(Url.GetAlipayStatus)
                .asString()
                .retryWhen(new RetryWithDelay())
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(string -> {

                }, throwable -> {

                });
    }
}